package ui;

import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.StringItem;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Command;
import agent.*;
import manager.AgentManagerMIDlet;

public class IdleScreen extends Form implements CommandListener{
	private Command confirmCommand;
	private Command exitCommand;
	private Command createAgentCommand;
	private Command refreshCommand;
	private Command handleIncomingAgentCommand;
	private AgentManagerMIDlet midlet;
	private Display display;
    private StringItem agentDisplay;
    private manager.IOHandlerManager ioHandler;
	private Agent received_agent;
    
	public IdleScreen(AgentManagerMIDlet midlet,Display display){
		super("Waiting to receive an agent.");
		this.display = display;
		this.midlet = midlet;
        confirmCommand = new Command("Confirm", Command.SCREEN,1);
        exitCommand = new Command("Exit", Command.EXIT, 1);
        createAgentCommand = new Command("Create new agent and send", Command.SCREEN,1);
        refreshCommand = new Command("Refresh",Command.SCREEN,1);
        this.addCommand(createAgentCommand);
        this.addCommand(confirmCommand);
        this.addCommand(exitCommand);
        this.addCommand(refreshCommand);
        this.setCommandListener(this);
        this.ioHandler = midlet.getIOHandlerManager();
    }
	
	
	public void commandAction(Command c, Displayable d){
		if(c==exitCommand){
			midlet.exitRequested();
		}
		else if (c==confirmCommand){
			
		}
		else if (c==createAgentCommand){
			AgentCreationScreen agentCreationScreen = 
				new AgentCreationScreen(midlet, "Agent Creation", display);
			display.setCurrent(agentCreationScreen);
		}
		else if(c==refreshCommand){
			if(ioHandler.isAgentWaiting()){
				received_agent = ioHandler.receiveAgent();

				if(received_agent != null && received_agent.getType().equals(Agent.TYPE_MEETING)){
					agentDisplay = new StringItem("Agent is : ",received_agent.toString());
					this.append(agentDisplay);
					handleIncomingAgentCommand = new Command("Handle incoming Agent",Command.SCREEN,1);
					this.addCommand(handleIncomingAgentCommand);
				}
				else if(received_agent != null && received_agent.getType().equals(Agent.TYPE_INFORMATION)){
					agent.InfoAgent infoAgent = (agent.InfoAgent)received_agent;
					agentDisplay = new StringItem("Information Agent has information : ", infoAgent.getMessage());
					this.append(agentDisplay);
					
					if(infoAgent.getNextAddressIndex()<infoAgent.sizeOfContacts()-1){
						midlet.getIOHandlerManager().sendAgent(infoAgent);
					}
					
				}
			}
		}else if(c==handleIncomingAgentCommand){
			if(received_agent.getType().equals(agent.Agent.TYPE_MEETING)){
				MeetingAgent tempAgent = new MeetingAgent();
				tempAgent.fromString(received_agent.toString());
				
				/* If not last contact in list, get choices of times. */
				if(tempAgent.getNextAddressIndex()<tempAgent.sizeOfContacts()-1){
					
					ReceiveAgentScreen receiveAgentScreen = new ReceiveAgentScreen(midlet,display,tempAgent);
					display.setCurrent(receiveAgentScreen);
				}
				else{
					InfoAgent infoAgent = new InfoAgent();
					infoAgent = tempAgent.convertToInfo();
					midlet.getIOHandlerManager().sendAgent(infoAgent);
				}
			}
			
			else if(received_agent.getType().equals(agent.Agent.TYPE_INFORMATION)){
				//Deal with confirmation
				agent.InfoAgent infoAgent = (agent.InfoAgent)received_agent;				
				agentDisplay = new StringItem("Information Agent has information : ", infoAgent.getMessage());
			}
		}
	}
}
